package com.yq.chaos.base.app;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;

/**
 * Created by njh on 2017/10/9.
 */

public class SplashAdWrapper implements Application.ActivityLifecycleCallbacks,ComponentCallbacks2 {
    private Application mApplication;
    private boolean mIsBackground=false;
    public SplashAdWrapper(Application application){
        this.mApplication=application;
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            //在Applition 上注册监听
            application.registerActivityLifecycleCallbacks(this);
            application.registerComponentCallbacks(this);
        }

    }
    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        if (mIsBackground){
            mIsBackground=false;
            //
            LogUtils.i("SplashAdWrapper app从后台进入前台");
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    /**
     * 对Application 监听进行解注
     * @param
     */
    public void release(){
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            //在Applition 上注册监听
            this.mApplication.registerActivityLifecycleCallbacks(this);
            this.mApplication.registerComponentCallbacks(this);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration configuration) {

    }

    @Override
    public void onLowMemory() {

    }



    @Override
    public void onTrimMemory(int level) {
        if (level==TRIM_MEMORY_UI_HIDDEN){
            Glide.get(mApplication).clearMemory();
            mIsBackground=true;
            LogUtils.i("SplashAdWrapper app退出到后台");
        }
        Glide.get(mApplication).trimMemory(level);
    }



}
